package com.lc.cehui.ui.fragment;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.cehui.R;
import com.lc.cehui.app.AppFragment;
import com.lc.cehui.http.glide.GlideApp;
import com.lc.cehui.http.request.LoginCompanyInfoApi;
import com.lc.cehui.http.request.LoginPersonInfoApi;
import com.lc.cehui.http.response.LoginCompanyInfoBean;
import com.lc.cehui.http.response.LoginPersonInfoBean;
import com.lc.cehui.other.IntentKey;
import com.lc.cehui.ui.activity.DitchPassActivity;
import com.lc.cehui.ui.activity.InterviewScheduleActivity;
import com.lc.cehui.ui.activity.MainActivity;
import com.lc.cehui.ui.activity.OrderListActivity;
import com.lc.cehui.ui.activity.ResumeReceivedActivity;
import com.lc.cehui.ui.activity.SettingActivity;
import com.lc.cehui.ui.activity.ShoppingCartActivity;
import com.lc.cehui.ui.activity.mine.AccountNumberActivity;
import com.lc.cehui.ui.activity.mine.AddressListActivity;
import com.lc.cehui.ui.activity.mine.CollectionActivity;
import com.lc.cehui.ui.activity.mine.CouponActivity;
import com.lc.cehui.ui.activity.mine.CouponManagementActivity;
import com.lc.cehui.ui.activity.mine.MemberActivity;
import com.lc.cehui.ui.activity.mine.MyCertificateActivity;
import com.lc.cehui.ui.activity.mine.MyResumeActivity;
import com.lc.cehui.ui.activity.mine.ResumeSubmittedActivity;
import com.lc.cehui.ui.activity.mine.StaffManageActivity;
import com.lc.cehui.ui.activity.release.MyReleaseActivity;
import com.lc.cehui.util.CourseUtils;
import com.lc.cehui.util.SPUtil;
import com.lc.cehui.util.SettingBar;

import org.jetbrains.annotations.NotNull;

import java.util.Arrays;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * author : Android 轮子哥
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2018/10/18
 * desc   : 我的 Fragment
 */
public final class MeFragment extends AppFragment<MainActivity> {
    private static final String TAG = "MeFragment";
    @BindView(R.id.iv_header_icon)
    AppCompatImageView ivHeaderIcon;
    @BindView(R.id.name_tv)
    TextView nameTv;
    @BindView(R.id.tv_company)
    TextView tvCompany;
    @BindView(R.id.tv_num1)
    TextView tvNum1;
    @BindView(R.id.tv_num2)
    TextView tvNum2;
    @BindView(R.id.tv_num3)
    TextView tvNum3;
    @BindView(R.id.tv_num4)
    TextView tvNum4;
    @BindView(R.id.rv_center)
    RecyclerView rvCenter;
    @BindView(R.id.rv_bottom)
    RecyclerView rvBottom;
    @BindView(R.id.ll_sdjl)
    LinearLayout llSdjl;

    @BindView(R.id.img_1)
    ImageView img1;
    @BindView(R.id.tv_open_vip)
    TextView tvOpenVip;

    @BindView(R.id.tv_receive)
    TextView tvReceive;
    @BindView(R.id.rl_top)
    RelativeLayout rlTop;
    @BindView(R.id.sb_yuangong)
    SettingBar sbYuangong;
    @BindView(R.id.sb_lianxi)
    SettingBar sbLianxi;
    @BindView(R.id.sb_set)
    SettingBar sbSet;
    @BindView(R.id.tv_num_gr1)
    TextView tvNumGr1;
    @BindView(R.id.tv_gr_num2)
    TextView tvGrNum2;
    @BindView(R.id.tv_gr_num3)
    TextView tvGrNum3;
    @BindView(R.id.ll_top_geren)
    LinearLayout llTopGeren;
    @BindView(R.id.ll_top_qiye)
    LinearLayout llTopQiye;


    private String[] kindbottom;
    private int spanCount;

    public static MeFragment newInstance() {
        return new MeFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_me;
    }

    @Override
    protected void initView() {
        String[] kinds = getResources().getStringArray(R.array.my_kind);
        rvCenter = findViewById(R.id.rv_center);
        rvCenter.setLayoutManager(new GridLayoutManager(getActivity(), 5));
        BaseQuickAdapter adapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_re_center_top, Arrays.asList(kinds)) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, String bean) {
                baseViewHolder.setText(R.id.tv_menu_title_home, bean);
                ImageView iv = (ImageView) baseViewHolder.findView(R.id.iv_menu_home);
                iv.setImageResource(getResources().obtainTypedArray(R.array.item_re_center_top_icon).getResourceId(baseViewHolder.getPosition(), 0));
            }
        };
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                Intent intent = new Intent(getContext(), OrderListActivity.class);
                switch (position) {
                    case 0:
                        intent.putExtra("comfrom", 0);
//                        待付款
                        break;
                    case 1:
                        intent.putExtra("comfrom", 1);
//                        待发货
                        break;
                    case 2:
                        intent.putExtra("comfrom", 2);
//                        待收货
                        break;
                    case 3:
                        intent.putExtra("comfrom", 4);
//                        待返还
                        break;
//                    退货退款
                    case 4:
                        intent.putExtra("comfrom", 5);
                        break;

                }

                startActivity(intent);
            }
        });
        rvCenter.setAdapter(adapter);

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        Log.e(TAG, "onResume: " );
        if (SPUtil.get(getContext(), IntentKey.RID, 0)==1) {
            llTopQiye.setVisibility(View.VISIBLE);
            llTopGeren.setVisibility(View.GONE);
            sbYuangong.setVisibility(View.VISIBLE);
            kindbottom = getResources().getStringArray(R.array.my_kind_bottom);
            spanCount = 4;
            login_company_info();
        } else {
            llTopQiye.setVisibility(View.GONE);
            llTopGeren.setVisibility(View.VISIBLE);
            sbYuangong.setVisibility(View.GONE);
            kindbottom = getResources().getStringArray(R.array.my_kind_bottom_gr);
            spanCount = 5;
            login_person_info();
        }



        rvBottom.setLayoutManager(new GridLayoutManager(getActivity(), spanCount));
        BaseQuickAdapter bottomadapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_re_center_top, Arrays.asList(kindbottom)) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, String bean) {
                baseViewHolder.setText(R.id.tv_menu_title_home, bean);
                ImageView iv = (ImageView) baseViewHolder.findView(R.id.iv_menu_home);

                if (SPUtil.get(getContext(), IntentKey.RID, 0)==1) {
                    iv.setImageResource(getResources().obtainTypedArray(R.array.my_kind_bottom_icon).getResourceId(baseViewHolder.getPosition(), 0));
                } else {
                    iv.setImageResource(getResources().obtainTypedArray(R.array.my_kind_bottom_icon_gr).getResourceId(baseViewHolder.getPosition(), 0));
                }

            }
        };
        bottomadapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                switch (position) {
                    case 0:
//                        我的收藏
                        startActivity(CollectionActivity.class);
                        break;
                    case 1:
//                        购物车
                        startActivity(ShoppingCartActivity.class);
                        break;
                    case 2:
//                        我的发布
                        if (SPUtil.get(getContext(), IntentKey.RID, 0)==1) {
                            startActivity(MyReleaseActivity.class);
                        } else {
//                            我的简历
                            startActivity(MyResumeActivity.class);
                        }

                        break;
                    case 3:
//                        地址管理
                        if (SPUtil.get(getContext(), IntentKey.RID, 0)==1) {
                            startActivity(AddressListActivity.class);
                        } else {
//                            我的证书
                            startActivity(MyCertificateActivity.class);
                        }

                        break;
//
                    case 4:
//                        购物车
                        startActivity(AddressListActivity.class);
                        break;
                }
            }
        });
        rvBottom.setAdapter(bottomadapter);
    }

    private void login_person_info() {
        EasyHttp.post(this)
                .api(new LoginPersonInfoApi().setUserid(SPUtil.get(getContext(), IntentKey.USERID,0)))
                .request(new HttpCallback<LoginPersonInfoBean>(this) {

                    @Override
                    public void onSucceed(LoginPersonInfoBean data) {

                        if (data.getCode().equals("200")){
                            GlideApp.with(getActivity())
                                    .load(data.getData().getPicurl())
                                    .placeholder(R.mipmap.img_head)
                                    .error(R.mipmap.img_head)
                                    .transform(new MultiTransformation<>(new CenterCrop(), new CircleCrop()))
                                    .into((ImageView) ivHeaderIcon);
                            nameTv.setText(data.getData().getName());
                            tvCompany.setText(data.getData().getMobile()+"");
                            tvNumGr1.setText(data.getData().getGet_resume()+"");
                            tvGrNum2.setText(data.getData().getLook_resume()+"");
                            tvGrNum3.setText(data.getData().getAll_resume()+"");

                        }

                    }

                });
    }

    private void login_company_info() {
        EasyHttp.post(this)
                .api(new LoginCompanyInfoApi().setUserid(SPUtil.get(getContext(), IntentKey.USERID,0)))
                .request(new HttpCallback<LoginCompanyInfoBean>(this) {

                    @Override
                    public void onSucceed(LoginCompanyInfoBean data) {

                        if (data.getCode().equals("200")){
                            GlideApp.with(getActivity())
                                    .load(data.getData().getPicurl())
                                    .placeholder(R.mipmap.img_head)
                                    .error(R.mipmap.img_head)
                                    .transform(new MultiTransformation<>(new CenterCrop(), new CircleCrop()))
                                    .into((ImageView) ivHeaderIcon);
                            nameTv.setText(data.getData().getName());
                            tvCompany.setText(data.getData().getCnname());
                            tvNum1.setText(data.getData().getGet_resume()+"");
                            tvNum2.setText(data.getData().getLook_resume()+"");
                            tvNum3.setText(data.getData().getAll_resume()+"");
                            tvNum4.setText(data.getData().getResult_resume()+"");
                        }

                    }

                });
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.e(TAG, "onStart: " );

    }

    @OnClick({R.id.setting_iv, R.id.tv_open_vip, R.id.tv_receive, R.id.sb_yuangong, R.id.sb_lianxi, R.id.sb_set, R.id.name_tv, R.id.iv_header_icon, R.id.tv_company,
            R.id.ll_sdjl, R.id.ll_gtg, R.id.ll_ms, R.id.ll_ytjl, R.id.ll_ckjl, R.id.ll_qbgt, R.id.ll_msap,R.id.sb_youhuijuan})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.setting_iv:
                startActivity(SettingActivity.class);
//                设置
                break;
            case R.id.tv_open_vip:
//                开通VIP
                startActivity(MemberActivity.class);

                break;
            case R.id.tv_receive:
//                立即领取
                startActivity(CouponActivity.class);

                break;
            case R.id.sb_yuangong:
//                企业员工
                startActivity(StaffManageActivity.class);
                break;
            case R.id.sb_lianxi:
//                联系我们
                break;
            case R.id.sb_set:
                //                设置
                startActivity(SettingActivity.class);
                break;
            case R.id.name_tv:
            case R.id.iv_header_icon:
            case R.id.tv_company:
//                账号资料
                startActivity(AccountNumberActivity.class);
                break;
            case R.id.ll_sdjl://企业-接收简历
                Intent intent = new Intent(getContext(), ResumeReceivedActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_gtg:
//                个人-沟通过
                startActivity(DitchPassActivity.class);
                break;
            case R.id.ll_ms:
                //                个人-面试
                startActivity(InterviewScheduleActivity.class);
                break;
            case R.id.ll_ytjl:
                //                个人-已投简历
                startActivity(ResumeSubmittedActivity.class);
                break;
            case R.id.ll_ckjl:
//                企业-查看简历
                break;
            case R.id.ll_qbgt:
//                企业-全部沟通
                break;
            case R.id.ll_msap:
//                企业-面试安排
                break;
            case R.id.sb_youhuijuan:
//                企业-优惠券管理
                startActivity(CouponManagementActivity.class);
                break;

        }
    }


}